<?php
namespace app\admin\controller;
use app\admin\controller\Base;
use think\Controller;
use think\Db;
use think\Request;
use think\facade\Env;
// use app\admin\Model\Gkdata;
class Product extends Base
{
	public function search(){
        $page=1;
        $request=new request();
        $sname=$request->post('sname');
        // $where['title'] = ['like','%'.$sname.'%'];
        $data=Db::table('news')->where('title','like','%'.$sname.'%')->select();
        $num=Db::table('news')->count('newsid');
        $pagenum=ceil($num/10);
        $this->assign('cpage',$page);
        $this->assign('pagenum',$pagenum);
        $this->assign('data',$data);
        return $this->fetch('index');
    }
    public function index($page=1)
    {
        $data=Db::table('product')->paginate(5);
        // $num=Db::table('news')->count('newsid');
        // $pagenum=ceil($num/10);
        $typedata=Db::name('type')->select();
        // $this->assign('cpage',$page);
        $this->assign('typedata',$typedata);
        $this->assign('data',$data);
        // $this->assign('pagenum',$pagenum);
        return $this->fetch();
    }
    // 进入添加商品页面
    public function addpage()
    {
        $data=Db::table('type')->select();
        $this->assign('data',$data);
        return $this->fetch();
    }
    // 添加商品
    public function addproduct()
    {
        $request=new request();
        $type=$request->post('type');
        $laiyuan=$request->post('laiyuan');
        $self=$request->post('self');
        $title=$request->post('title');
        $keywords=$request->post('keywords');
        $description=$request->post('description');
        $lunbo=$request->post('lunbo');
        $hot=$request->post('hot');
        // 实例化上传图片的对象
        $upimgobj=controller('Common');
        $image_error = $_FILES['thumb']['error'];
        if ($image_error == 4) {
            $thumb='';
        }else{
            $file=$request->file('thumb');
            // 为什么不执行下行代码，因为php上传文件后较短时间会删除临时文件，也就是$_FILES[‘file’][‘tmp_name’]这个值，这样我下方的fiel('bnnerimg')就会找不到临时文件，然后报错。真的是日了狗
            // $thumb=$upimgobj->upaction1($file);          
        }
        $image_error2 = $_FILES['bannerimg']['error'];
        if ($image_error2 == 4) {
            $bannerimg='';
        }else{
            $file2=$request->file('bannerimg');
            // $bannerimg=$upimgobj->upaction1($file2);
        }
        // $rpath=Env::get('root_path');
        if(!empty($file)){           
            $thumb=$upimgobj->upaction1($file);
        }
        if(!empty($file2)){
            $bannerimg=$upimgobj->upaction1($file2);
        }
        $content=$request->post('content1');
        $status=$request->post('status');
        $data=[
            "ntid"=>$type,
            "laiyuan"=>$laiyuan,
            "self"=>$self,
            "title"=>$title,
            "keywords"=>$keywords,
            "description"=>$description,
            "lunbo"=>$lunbo, 
            "hot"=>$hot,           
            "thumb"=>$thumb,
            "bannerimg"=>$bannerimg,
            "content"=>$content,
            "status"=>$status,
            "time"=>time()
        ];
        $re=Db::name('news')->insert($data);
        if($re){
            $this->success('新增成功', 'News/index');
        }else{
            return $this->error('插入失败');
        }

    }
    // 修改启用状态
    public function stchangenews(){
        $request=new request();
        $newsid= intval($request->get("id"));
        $status=intval($request->get("status"));
        if($status==0){
            $status=1;
        }else{
            $status=0;
        }
        $data=[
            "newsid"=>$newsid,
            "status"=>$status
        ];
        $da=Db::name('news')->update($data);
        if($da){
            $this->success('状态修改成功', 'news/index');
        }
        else{
            return '更新失败'.$da;
        }
    }
    // 进入修改资讯页面
    public function editpage(){
        $request=new request();
        $newsid= intval($request->get("id"));
        $data=Db::name('news')->where('newsid',$newsid)->find();
        $typedata=Db::name('newstype')->select();
        $this->assign('data',$data);
        $this->assign('typedata',$typedata);
        return $this->fetch();
    }
    // 修改资讯内容
    public function editnews()
    {
        $request=new request();
        $data=[];
        $newsid=$request->post('bianhao');
        $data['newsid']=$newsid;
        // 查找该资讯并对照表单数据
        $data1=Db::table('news')->where('newsid',$newsid)->find(); 
        $type=$request->post('type');
        if($type != $data1['ntid']){
            $data['ntid']=$type;
        }
        $laiyuan=$request->post('laiyuan');
        if($laiyuan != $data1['laiyuan']){
            $data['laiyuan']=$laiyuan;
        }
        $self=$request->post('self');
        if($self != $data1['self']){
            $data['self']=$self;
        }
        $title=$request->post('title');
        if($title != $data1['title']){
            $data['title']=$title;
        }
        $keywords=$request->post('keywords');
        if($keywords != $data1['keywords']){
            $data['keywords']=$keywords;
        }
        $description=$request->post('description');
        if($description != $data1['description']){
            $data['description']=$description;
        }
        $lunbo=$request->post('lunbo');
        if($lunbo != $data1['lunbo']){
            $data['lunbo']=$lunbo;
        }
        $hot=$request->post('hot');
        if($hot != $data1['hot']){
            $data['hot']=$hot;
        }
        // 实例化上传图片的对象
        $upimgobj=controller('Common');
        $image_error = $_FILES['thumb']['error'];
        if ($image_error != 4) {
            $file=$request->file('thumb');
        }
        $image_error2 = $_FILES['bannerimg']['error'];
        if ($image_error2 != 4) {
            $file2=$request->file('bannerimg');
        }
        if(!empty($file)){           
            $thumb=$upimgobj->upaction1($file);
            $data['thumb']=$thumb;
        }
        if(!empty($file2)){
            $bannerimg=$upimgobj->upaction1($file2);
            $data['bannerimg']=$bannerimg;
        }

        $content=$request->post('content1');
        if($content != $data1['content']){
            $data['content']=$content;
        }
        $status=$request->post('status');
        if($status != $data1['status']){
            $data['status']=$status;
        }
        // var_dump($data);
        $da=Db::name('news')->update($data);
        if($da){
            $this->success('修改成功', 'news/index');
        }
        else{
            return $this->error('更新失败');
        }

    }


    // 进入资讯类型列表页面
    public function typeindex(){
        $data=Db::table('type')->select();
        $this->assign('data',$data);
        return $this->fetch();
    }
    // 进入添加资讯类型页面
    public function ntype()
    {
        $data=Db::table('type')->select();
        $this->assign('data',$data);
        return $this->fetch('addtype');
    }
    // 添加资讯类型
    public function addtype()
    {
        $request=new request();
        $ntname=$request->post("title");
        $topid=$request->post("topid");
        if(empty($topid)){
            $topid=-1;
        }else{
            $topid=(int)$topid;
        }
        $data=[
        "tname"=>$ntname,
        "topid"=>$topid
        ];
        $re=Db::name('type')->insert($data);
        if($re){
            $this->success('新增成功', 'Product/typeindex');
        }else{
            return $this->error('插入失败');
        }
    }
    // 进入更改资讯类型页面
    public function edtype()
    {
        $request=new request();
        $ntid=$request->get("id");
        $data=Db::name('newstype')->where('ntid',$ntid)->find();
        $this->assign('data',$data);
        return $this->fetch();

    }
    //更改资讯类型 
    public function edittype()
    {
        $request=new request();
        $ntid=$request->post("ntid");
        $ntname=$request->post("title");
        $data=[
            "ntid"=>$ntid,
            "ntname"=>$ntname
        ];
        $da=Db::name('newstype')->update($data);
        if($da){
            $this->success('修改成功', 'news/typeindex');
        }
        else{
            return $this->error('更新失败');
        }
    }  
     //删除资讯类型 
    public function delnewstype()
    {
        $request=new request();
        $ntid=$request->get("id");
        $da=Db::name('newstype')->delete($ntid);
        if($da){
            $this->success('删除成功', 'news/typeindex');
        }
        else{
            return $this->error('删除失败');
        }
    }
     //删除资讯
    public function delnews()
    {
        $request=new request();
        $newsid=$request->get("id");
        $da=Db::name('news')->delete($newsid);
        if($da){
            $this->success('删除成功', 'news/index');
        }
        else{
            return $this->error('删除失败');
        }
    }
}
